package com.yangtzeu.jwc.businessservice.impl;

import android.widget.ImageView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.yangtzeu.jwc.app.MyApplication;
import com.yangtzeu.jwc.utils.AnimateFirstDisplayListener;


public class LoaderBusiness {
    private static LoaderBusiness loaderBusiness = new LoaderBusiness();
    public ImageLoader imageLoader;
    private DisplayImageOptions options;
    private AnimateFirstDisplayListener animateFirstListener;

    private LoaderBusiness() {
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(MyApplication.getInstance()));
        animateFirstListener = new AnimateFirstDisplayListener();

        options = new DisplayImageOptions.Builder()
                //.showImageOnLoading(R.drawable.picture_loading)
                //.showImageForEmptyUri(R.drawable.picture_loading)
                //.showImageOnFail(R.drawable.picture_loading)
                //.cacheInMemory(true)
                .cacheOnDisc(true)
                .considerExifParams(true)
                        //.displayer(new RoundedBitmapDisplayer(0))
                .build();
    }

    ;

    public static LoaderBusiness getInstance() {
        return loaderBusiness;
    }

    public ImageLoader getImageLoader() {
        return imageLoader;
    }

    public DisplayImageOptions getOptions() {
        return options;
    }

    public AnimateFirstDisplayListener getAnimateFirstListener() {
        return animateFirstListener;
    }

    public static void loadImage(String url, ImageView iv) {
        DisplayImageOptions options = getInstance().getOptions();
        ImageLoadingListener animateFirstListener = getInstance().getAnimateFirstListener();
        getInstance().getImageLoader().displayImage(url, iv, options, animateFirstListener);
    }

    public static void loadImage(String url, ImageView iv, int drawableId) {
        getInstance().options = new DisplayImageOptions.Builder()
                .showImageOnLoading(drawableId)
                .showImageForEmptyUri(drawableId)
                .showImageOnFail(drawableId)
                        //.cacheInMemory(true)
                .cacheOnDisc(true)
                .considerExifParams(true)
                        //.displayer(new RoundedBitmapDisplayer(0))
                .build();
        loadImage(url, iv);
    }
}
